package com.example.travel_front.Activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.travel_front.Adapter.HourlyForecastAdapter
import com.example.travel_front.Model.HourlyForecast
import com.example.travel_front.R
import com.mapbox.geojson.Point
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.maps.extension.style.image.image
import com.mapbox.maps.extension.style.layers.generated.symbolLayer
import com.mapbox.maps.extension.style.layers.properties.generated.IconAnchor
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
import com.mapbox.maps.extension.style.style

class DiscoverActivity : AppCompatActivity() {

    private var mapView: MapView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.map_detail)
        // Initialize your forecast list here
        val forecastList = getHourlyForecasts() // This should return List<HourlyForecast>
        val recyclerView: RecyclerView = findViewById(R.id.hourlyForecastRecyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        val adapter = HourlyForecastAdapter(forecastList)
        recyclerView.adapter = adapter

        mapView = findViewById(R.id.mapView)

        mapView?.getMapboxMap()?.also {
            it.setCamera(
                CameraOptions.Builder()
                    .center(Point.fromLngLat(LONGITUDE, LATITUDE))
                    .zoom(10.0)
                    .build()
            )
        }?.loadStyle(
            styleExtension = style(Style.STANDARD) {
                // prepare blue marker from resources
                +image(
                    BLUE_ICON_ID,
                    ContextCompat.getDrawable(this@DiscoverActivity, R.drawable.red_marker)!!.toBitmap()
                )
                +geoJsonSource(SOURCE_ID) {
                    geometry(Point.fromLngLat(LONGITUDE, LATITUDE))
                }
                +symbolLayer(LAYER_ID, SOURCE_ID) {
                    iconImage(BLUE_ICON_ID)
                    iconAnchor(IconAnchor.BOTTOM)
                    iconSize(0.5)
                }
            }
        )
    }

    @SuppressLint("Lifecycle")
    override fun onStart() {
        super.onStart()
        mapView?.onStart()
    }

    @SuppressLint("Lifecycle")
    override fun onStop() {
        super.onStop()
        mapView?.onStop()
    }

    @SuppressLint("Lifecycle")
    override fun onDestroy() {
        super.onDestroy()
        mapView?.onDestroy()
    }

    @SuppressLint("Lifecycle")
    override fun onLowMemory() {
        super.onLowMemory()
        mapView?.onLowMemory()
    }


    private fun getHourlyForecasts(): List<HourlyForecast> {
        val forecastList: MutableList<HourlyForecast> = ArrayList()

        // Adding some sample data
        forecastList.add(HourlyForecast("6 AM", R.drawable.ic_fine, "20°C"))
        forecastList.add(HourlyForecast("7 AM", R.drawable.ic_cloudy, "21°C"))
        forecastList.add(HourlyForecast("8 AM", R.drawable.ic_cloudy, "22°C"))
        forecastList.add(HourlyForecast("9 AM", R.drawable.ic_cloudy, "18°C"))
        forecastList.add(HourlyForecast("10 AM", R.drawable.ic_blizzard, "17°C"))
        forecastList.add(HourlyForecast("11 AM", R.drawable.ic_cloudy, "18°C"))
        forecastList.add(HourlyForecast("12 AM", R.drawable.ic_blizzard, "17°C"))
        return forecastList
    }

    companion object {
        private const val BLUE_ICON_ID = "blue"
        private const val SOURCE_ID = "source_id"
        private const val LAYER_ID = "layer_id"
        private const val LONGITUDE = -74.0060
        private const val LATITUDE = 40.7128
    }
}
